package com.fau.ss.service.impl;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fau.ss.entity.MedicalAdmin;
import com.fau.ss.entity.MedicalSsPerformance;
import com.fau.ss.service.ICenterperformanceStaService;
import com.fau.ss.util.DateUtil;
import com.fau.ss.util.Page;
import com.fau.ss.util.ParamUtil;

@Service
@Transactional(readOnly = true)
public class CenterPerformanceStaService extends BaseService implements ICenterperformanceStaService {

	@Override
	public Page findCenterperformanceSta(Map<String, Object> map, MedicalSsPerformance performance, MedicalAdmin admin) {
		StringBuilder statusSql = new StringBuilder("select a.platformName, COUNT(a.orderId) as num, SUM(a.orderMoney) as money from MedicalSsPerformance a where 1=1 and a.hospitalId = ? "); 
		StringBuilder countSql = new StringBuilder("select count(distinct a.platformId) from MedicalSsPerformance a where a.hospitalId = ? ");
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getEnterpriseId());
		Date startTime = null;
		Date endTime = null;
		if(StringUtils.isNotBlank(performance.getStartTime()) && StringUtils.isNotBlank(performance.getEndTime())) {
			startTime = DateUtil.getDate(performance.getStartTime());
			endTime = DateUtil.getDate(performance.getEndTime());
			statusSql.append("and a.createTime between ? and ? ");
			countSql.append("and a.createTime between ? and ? ");
			objectList.add(startTime);
			objectList.add(endTime);
		}
		
		statusSql.append("group by a.platformId order by money desc");
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map, "pageIndex", 1), ParamUtil.getInteger(map, "pageSize", 20), map, countSql.toString(), statusSql.toString(), objectList);
		List<MedicalSsPerformance> list = new ArrayList<MedicalSsPerformance>();
		for(Object obj : page.getResult()){
			MedicalSsPerformance bean = new MedicalSsPerformance();
			Object[] objBean =  (Object[]) obj;
			if(null != objBean[0]) {
				bean.setPlatformName(objBean[0].toString());
				bean.setCountOrders((Long)objBean[1]);
				BigDecimal summoney = new BigDecimal(objBean[2].toString());
				bean.setOrderSumMoney(summoney);
				list.add(bean);
			}
		}
		page.setResult(list);
		return page;
	}

}
